import nats from 'node-nats-streaming';
import { randomBytes } from 'crypto';
import { TicketCreatedListener } from './events/ticket-created-listener';
import { TicketUpdatedListener } from './events/ticket-updated-listener';

console.clear();
const stan = nats.connect('ticketing', randomBytes(4).toString('hex'), {
    url: 'http://localhost:4222',
});


stan.on('connect', () => {
    console.log('Listener connected to NATS');

    stan.on('close', () => {
        console.log('NATS connection closed!')
        process.exit();//关闭进程，告知推送服务(publish)这个监听进程已经不存在了
    });

    new TicketCreatedListener(stan).listen();
    new TicketUpdatedListener(stan).listen();
});

//监听到中断信息或终止信息就会试着关闭客户端监听服务（listener)
process.on('SIGINT', () => stan.close());
process.on('SIGTERM', () => stan.close());